package com.yanqu.road.dao.impl.goods;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.goods.MallRandomGoods;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class MallRandomGoodsDaoImpl extends TempDataObjectDao<MallRandomGoods> {

    public Map<Integer, MallRandomGoods> getGoodsInfoMap() {
        Map<Integer, MallRandomGoods> dataMap = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_mall_random_goods";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                MallRandomGoods data = parse(rs);
                dataMap.put(data.getShopType(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(MallRandomGoods data) {
        PreparedStatement call = null;
        String sql = "INSERT INTO t_u_mall_random_goods(shopType,goodsInfo,lastUpdateTime"
                + ") VALUES(?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getShopType());
            call.setString(i++, StringUtils.listToString(data.getMallIdList(), ","));
            call.setLong(i++, data.getLastUpdateTime());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(MallRandomGoods data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_mall_random_goods set goodsInfo=?,lastUpdateTime=?"
                + " where shopType=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setString(i++, StringUtils.listToString(data.getMallIdList(), ","));
            call.setLong(i++, data.getLastUpdateTime());
            call.setInt(i++, data.getShopType());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected MallRandomGoods parseData(ResultSet rs) throws SQLException {
        MallRandomGoods data = new MallRandomGoods();
        data.setShopType(rs.getInt("shopType"));
        data.setMallIdList(StringUtils.stringToIntegerList(rs.getString("goodsInfo"), ","));
        data.setLastUpdateTime(rs.getLong("lastUpdateTime"));
        return data;
    }
}
